package dshop.action.admin;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;

import appbase.jfinal.ext.ApiBaseController;
import dshop.interceptor.ApiAuthInterceptor;
import dshop.interceptor.ApiReadBodyParamInterceptor;

@Before({ ApiReadBodyParamInterceptor.class, ApiAuthInterceptor.class })
public class OrderAction extends ApiBaseController {

	public void refundOrderList() {
		int status = getParaToInt("status", -1);
		int refundIntoTime = Db.findFirst("select * from store_setting").getInt("refund_into_time");
		String select = " SELECT sr.id,sr.refund_explain,sr.refund_cause,sr.refund_money,sr.order_id,a.id account_id,sr.status,sr.remark,sr.transfer_status,sc.`name`,st.service_phone,a.phone,CONCAT(mgi.last_name,mgi.first_name) username,dc.bank,dc.account_name,dc.account_no, "
				+ " DATE_FORMAT(sr.consent_refund_time,'%Y-%m-%d') consent_refund_time, "
				+ " DATE_FORMAT(sr.add_time,'%Y-%m-%d') add_time,"
				+ " IF(TIMESTAMPDIFF(DAY,DATE_FORMAT(sr.consent_refund_time, '%Y-%m-%d'),DATE_FORMAT(NOW(), '%Y-%m-%d')) >= "+refundIntoTime+", 1 ,0 ) is_expire ";
		String from = " FROM store_refund sr LEFT JOIN store_company sc on sc.store_id = sr.store_id "
				+ "LEFT JOIN store st on st.id = sc.store_id " + "LEFT JOIN account a on a.id = sr.account_id "
				+ "LEFT JOIN member_general_info mgi on a.id = mgi.account_id "
				+ "LEFT JOIN draw_card dc on (dc.user_id = a.id and dc.`status` = 1) ";
		String where = " where 1 = 1  ";
		List<Object> params = new ArrayList<Object>();
		if (status >= 0) {
			if(3 == status) {//待打款
				where += " and sr.status = 1 and transfer_status = 0 ";
			}else if(4 == status) {//已打款
				where += " and sr.status = 1 and transfer_status = 1 ";
			}else {
				where += " and sr.status = ? ";
				params.add(status);
			}
		}
		String orderBy = " order by sr.add_time desc";
		Page<Record> page = page(select, from + where + orderBy, params.toArray());

		renderSuccess();
	}
	public static void main(String[] args) {
		System.out.println(new BigDecimal(990).divide(new BigDecimal(100)));
		System.out.println(new BigDecimal(6).divide(new BigDecimal(1000),3,BigDecimal.ROUND_HALF_UP));
		System.out.println(new BigDecimal(990).divide(new BigDecimal(100)).multiply(new BigDecimal(6).divide(new BigDecimal(1000),3,BigDecimal.ROUND_HALF_UP)).multiply(new BigDecimal(100)).setScale(0,BigDecimal.ROUND_HALF_UP));
	}
	@Before(Tx.class)
	public void refundOrderTransfer() {
		int id = getParaToInt("id");
		int rate = Db.findFirst("select * from store_setting").getInt("rate");
		Record record = Db.findFirst("select st.security_deposit,sr.refund_money,sr.store_id FROM store_refund sr LEFT JOIN store st on sr.store_id = st.id  WHERE sr.id = "+id);
		int securityDeposit = record.getInt("security_deposit");
		int refundMoney = record.getInt("refund_money");
		int storeId = record.getInt("store_id");
		// 计算扣除多少保证金
		int deduction =new BigDecimal(refundMoney).divide(new BigDecimal(100)).multiply(new BigDecimal(rate).divide(new BigDecimal(1000),3,BigDecimal.ROUND_HALF_UP)).multiply(new BigDecimal(100)).setScale(0,BigDecimal.ROUND_HALF_UP).intValue();
		Db.update("update store set security_deposit = "+(securityDeposit - deduction)+" where id = "+ storeId);
		List<Object> params = new ArrayList<Object>();
		params.add(id);
		Db.update("update store_refund set transfer_status = 1 where id = ?", params.toArray());
		renderSuccess();
	}
}
